package com.lxr0860.lxr0860production_test.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.lxr0860.lxr0860production_test.utils.Contants;

import java.util.ArrayList;
import java.util.List;

/**
 * 重复弹窗
 * Created by uniriho on 2017/9/4.
 */

public class TouchPadView extends View {
    private final String TAG = "TouchPadView";
    private Activity mActivity;
    /**
     * screen's height
     */
    private int mScreenHeight;
    /**
     * screen's width
     */
    private int mScreenWidth;
    /**
     * lattice's height
     */
    private final int mGridHeight = 80;//画TP图高度方向影响
    /**
     * lattice's width
     */
    private final int mGridWidth = 60;
    /**
     * Each Row can has the number of lattice
     */
    private int mColCount;
    /**
     * Each column can has the number of lattice
     */
    private int mRowCount;
    private List<Rect> mLineTop;
    private List<Rect> mLineBottom;
    private List<Rect> mLineLeft;
    private List<Rect> mLineRight;
    private List<Rect> mLineMH; //middle horonzental
    private List<Rect> mLineMV;  // middle vertical
    private boolean[] isTouchTop;
    private boolean[] isTouchBottom;
    private boolean[] isTouchLeft;
    private boolean[] isTouchRight;
    private boolean[] isTouchMH;
    private boolean[] isTouchMV;
    private final Paint mPaintLine;
    private final Paint mPaintFill;


    public TouchPadView(Activity activity) {
        super(activity);
        this.mActivity = activity;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        this.mPaintLine = new Paint();
        mPaintFill = new Paint();
        initPaints();
        initLines();
    }

    private void initPaints() {
        // TODO Auto-generated method stub
        mPaintLine.setColor(Color.WHITE);
        mPaintLine.setStyle(Paint.Style.STROKE);
        mPaintLine.setStrokeWidth(2F);
        mPaintFill.setColor(Color.GREEN);
        mPaintFill.setStyle(Paint.Style.FILL);
    }

    private void initLines() {
        getScreenHeightAndWidth();
        mLineTop = new ArrayList<Rect>();
        mLineBottom = new ArrayList<Rect>();
        mLineLeft = new ArrayList<Rect>();
        mLineRight = new ArrayList<Rect>();
        mLineMH = new ArrayList<Rect>();
        mLineMV = new ArrayList<Rect>();
        mColCount = (int) Math.ceil(mScreenWidth * 1.0F / mGridWidth);
        mRowCount = (int) Math.ceil(mScreenHeight * 1.0F / mGridHeight);
        for (int i = 0; i < mColCount; i++) {
            mLineTop.add(new Rect(i * mGridWidth, 0, (i + 1) * mGridWidth, mGridHeight));
        }
        for (int i = 0; i < mColCount; i++) {
            mLineBottom.add(new Rect(i * mGridWidth, mScreenHeight - mGridHeight, (i + 1) * mGridWidth, mScreenHeight));
        }
        for (int i = 1; i < mRowCount - 1; i++) {
            mLineLeft.add(new Rect(0, i * mGridHeight, mGridWidth, (i + 1) * mGridHeight));
        }
        for (int i = 1; i < mRowCount - 1; i++) {
            mLineRight.add(new Rect(mScreenWidth - mGridWidth, i * mGridHeight, mScreenWidth, (i + 1) * mGridHeight));
        }
        for (int i = 1; i < mColCount - 1; i++) {
            mLineMH.add(new Rect(i * mGridWidth, mScreenHeight / 2 - mGridHeight, (i + 1) * mGridWidth, mScreenHeight / 2));
        }
        for (int i = 1; i < mRowCount - 1; i++) {
            mLineMV.add(new Rect(mScreenWidth / 2 - mGridWidth, i * mGridHeight, mScreenWidth / 2, (i + 1) * mGridHeight));
        }
        isTouchTop = new boolean[mLineTop.size()];
        for (int i = 0; i < isTouchTop.length; i++) isTouchTop[i] = false;
        isTouchBottom = new boolean[mLineBottom.size()];
        for (int i = 0; i < isTouchBottom.length; i++) isTouchBottom[i] = false;
        isTouchLeft = new boolean[mLineLeft.size()];
        for (int i = 0; i < isTouchLeft.length; i++) isTouchLeft[i] = false;
        isTouchRight = new boolean[mLineRight.size()];
        for (int i = 0; i < isTouchRight.length; i++) isTouchRight[i] = false;
        isTouchMH = new boolean[mLineMH.size()];
        for (int i = 0; i < isTouchMH.length; i++) isTouchMH[i] = false;
        isTouchMV = new boolean[mLineMV.size()];
        for (int i = 0; i < isTouchMV.length; i++) isTouchMV[i] = false;
    }

    /**
     * Get the screen's height and Width
     */
    private void getScreenHeightAndWidth() {
        DisplayMetrics displayMestrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(
                displayMestrics);
        mScreenHeight = displayMestrics.heightPixels - 30;//画TP图高度方向影响，距屏幕下方的距离，-的月多，距离越远
        mScreenWidth = displayMestrics.widthPixels;
        Log.v(TAG, "mScreenHeight = " + mScreenHeight);
        Log.v(TAG, "mScreenWidth = " + mScreenWidth);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        for (int i = 0; i < isTouchTop.length; i++) {
            canvas.drawRect(mLineTop.get(i), mPaintLine);
            canvas.drawRect(mLineBottom.get(i), mPaintLine);
            if (isTouchTop[i]) {
                canvas.drawRect(mLineTop.get(i), mPaintFill);
            }
            if (isTouchBottom[i]) {
                canvas.drawRect(mLineBottom.get(i), mPaintFill);
            }
        }
        for (int i = 0; i < isTouchLeft.length; i++) {
            canvas.drawRect(mLineLeft.get(i), mPaintLine);
            canvas.drawRect(mLineRight.get(i), mPaintLine);
            if (isTouchLeft[i]) {
                canvas.drawRect(mLineLeft.get(i), mPaintFill);
            }
            if (isTouchRight[i]) {
                canvas.drawRect(mLineRight.get(i), mPaintFill);
            }
        }
        final int size_mh = isTouchMH.length;
        final int size_mv = isTouchMV.length;
        for (int i = 0; i < size_mh; i++) {
            canvas.drawRect(mLineMH.get(i), mPaintLine);
            if (isTouchMH[i]) {
                canvas.drawRect(mLineMH.get(i), mPaintFill);
            }
        }
        for (int i = 0; i < size_mv; i++) {
            canvas.drawRect(mLineMV.get(i), mPaintLine);
            if (isTouchMV[i]) {
                canvas.drawRect(mLineMV.get(i), mPaintFill);
            }
        }
    }

    private boolean allRectChange() {
        for (int i = 0; i < isTouchTop.length; i++) {
            if (!isTouchTop[i]) {
                System.out.println("isTouchTop" + " : " + i);
                return false;
            }
            if (!isTouchBottom[i]) {
                System.out.println("isTouchBottom" + " : " + i);
                return false;
            }
        }
        for (int i = 0; i < isTouchLeft.length; i++) {
            if (!isTouchLeft[i]) {
                System.out.println("isTouchLeft" + " : " + i);
                return false;
            }
            if (!isTouchRight[i]) {
                System.out.println("isTouchRight" + " : " + i);
                return false;
            }
        }
        for (int i = 0; i < isTouchMH.length; i++) {
            if (!isTouchMH[i]) {
                return false;
            }
        }
        for (int i = 0; i < isTouchMV.length; i++) {
            if (!isTouchMV[i]) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int touchX = (int) event.getX();
        int touchY = (int) event.getY();
        if (touchX == mScreenWidth) {
            touchX = touchX - 1;
        }
        if (touchY == mScreenHeight) {
            touchY = touchY - 1;
        }
        Log.v(TAG, "touchX = " + touchX);
        Log.v(TAG, "touchY = " + touchY);
        touchWhere(touchX, touchY);
        invalidate();

        if (allRectChange()) {
            if(builder==null){
              builder=  Contants.showDailog(mActivity,"是否保存此次测试结果", handler, Gravity.CENTER);
            }
            return true;
        }
        return true;
    }

    private void touchWhere(int touchX, int touchY) {
        System.out.println("[x, y] = [" + touchX + ", " + touchY + "]");

        for (int i = 0; i < isTouchTop.length; i++) {
            if (mLineTop.get(i).contains(touchX, touchY)) {
                isTouchTop[i] = true;
            }
            if (mLineBottom.get(i).contains(touchX, touchY)) {
                isTouchBottom[i] = true;
            }
        }

        for (int i = 0; i < isTouchLeft.length; i++) {
            if (mLineLeft.get(i).contains(touchX, touchY)) {
                isTouchLeft[i] = true;
            }
            if (mLineRight.get(i).contains(touchX, touchY)) {
                isTouchRight[i] = true;
            }
        }
        for (int i = 0; i < isTouchMH.length; i++) {
            if (mLineMH.get(i).contains(touchX, touchY)) {
                isTouchMH[i] = true;
            }
        }
        for (int i = 0; i < isTouchMV.length; i++) {
            if (mLineMV.get(i).contains(touchX, touchY)) {
                isTouchMV[i] = true;
            }
        }
    }


    /**
     * 测试成功后弹窗
     */
    private CustomDialog.Builder builder;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Contants.DIALOG_POSITIVE:
                    Intent mIntent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putBoolean("result",true);
                    mIntent.putExtras(bundle);
                    mActivity.setResult(Contants.TEST_ITEM_TP,mIntent);
                    mActivity.finish();
                    break;
                case Contants.DIALOG_NEGATIVE:
                    mActivity.finish();
                    break;
            }
        }
    };

}
